package com.digital.herbarium.Config;

import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.Exception.CustomException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public class MyExceptionHandler {

    /**
     * 捕获我们自定义的异常
     * @param
     * @return
     */
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult notLoginExceptionHandler(CustomException customException){
        return ResponseResult.FAILED(customException.getMessage()).setData(null);
    }

    /**
     * 捕获其他异常
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult otherException(Exception exception){
         // System.out.println(exception.toString());

        return ResponseResult.SERVER_ERROR().setData(exception);
    }

    /**
     * 捕获404
     * @param exception
     * @return
     */

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public ResponseResult otherException(NoHandlerFoundException exception){
        return ResponseResult.NOT_FOUND().setData(exception.getMessage());
    }
}
